package com.zmn.oms.services.impl.dict;

import com.google.common.collect.Lists;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.oms.model.entity.dict.ChannelZhimiFaultPart;
import com.zmn.oms.persistence.interfaces.dict.ChannelZhimiFaultPartDAO;
import com.zmn.oms.services.interfaces.dict.ChannelZhimiFaultPartService;
import org.apache.commons.collections4.ListUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author sunlife
 * @date:2020/5/9 11:50 上午
 * description:
 */
@Service
public class ChannelZhimiFaultPartServiceImpl implements ChannelZhimiFaultPartService {

    @Resource
    private ChannelZhimiFaultPartDAO channelZhimiFaultPartDAO;


    @Override
    public List<ChannelZhimiFaultPart> listByFaultCodeLv3(String faultCodeLv3, String thirdModelCode) {
        if (StringUtil.isBlank(faultCodeLv3)) {
            return Lists.newArrayList();
        }
        ChannelZhimiFaultPart query = new ChannelZhimiFaultPart();
        query.setFaultCodeLv3(faultCodeLv3);

        if (StringUtil.isNotBlank(thirdModelCode)) {
            query.setProductCode(thirdModelCode);
        }

        return channelZhimiFaultPartDAO.query(query);
    }

    /**
     * 通过三级故障编码查询对应的配件数量
     * @param faultCodeLv3List
     * @return
     */
    @Override
    public int countByFaultCodeLv3List(List<String> faultCodeLv3List, String thirdModelCode) {
        return channelZhimiFaultPartDAO.countByFaultCodeLv3List(faultCodeLv3List, thirdModelCode);
    }

    @Override
    public ChannelZhimiFaultPart findByPartCode(String partCode, String thirdModelCode) {
        if (StringUtil.isBlank(partCode)) {
            return null;
        }

        return channelZhimiFaultPartDAO.findByPartCode(partCode, thirdModelCode);
    }

    @Override
    public void saveList(List<ChannelZhimiFaultPart> partlist) {

        // 删除数据库中已有数据
        channelZhimiFaultPartDAO.deleteAll();

        if (partlist.size() <= 300) {
            channelZhimiFaultPartDAO.insertBatch(partlist);
        } else {
            List<List<ChannelZhimiFaultPart>> partition =ListUtils.partition(partlist, 300);
            partition.forEach(items -> channelZhimiFaultPartDAO.insertBatch(items));
        }
    }

    /**
     * 查询指定三级机型的所有故障列表
     * @param thirdModelCode
     * @return
     */
    @Override
    public List<ChannelZhimiFaultPart> listFaultsByThirdModeCode(String thirdModelCode) {
        if (StringUtil.isBlank(thirdModelCode)) {
            return null;
        }

        return channelZhimiFaultPartDAO.listFaultsByThirdModeCode(thirdModelCode);
    }
}
